package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.ReturnTuition;
import com.example.result.Result;
import com.example.service.ReturnTuitionService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 退班费控制层
 * @date 2020-02-20
 */
@RestController
@RequestMapping("/api")
public class ReturnTuitionController {

    @Autowired
    ReturnTuitionService returnTuitionService;

    // ===============================退班费管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加退班费信息
     */
    @RequestMapping(value = "/returns", method = RequestMethod.POST)
    public Result addReturnTuition(@RequestBody String json) {
        // 将JSON数据转换为ReturnTuition对象实体
        JSONObject parseObject = JSON.parseObject(json);
        ReturnTuition returnTuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"),
                ReturnTuition.class);

        return returnTuitionService.addReturnTuition(returnTuition);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新退班费信息
     */
    @RequestMapping(value = "/returns", method = RequestMethod.PUT)
    public Result updateReturnTuition(@RequestBody String json) {
        // 将JSON数据转换为ReturnTuition对象实体
        JSONObject parseObject = JSON.parseObject(json);
        ReturnTuition returnTuition = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"),
                ReturnTuition.class);
        return returnTuitionService.updateReturnTuition(returnTuition);

    }

    /**
     * @param returnTuitionId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据退班费编号删除退班费信息
     */
    @RequestMapping(value = "/returns/{returnTuitionId}", method = RequestMethod.DELETE)
    public Result deleteReturnTuitionById(@PathVariable("returnTuitionId") String returnTuitionId) {
        return returnTuitionService.deleteReturnTuitionById(returnTuitionId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有退班费信息
     */
    @RequestMapping(value = "/returns", method = RequestMethod.GET)
    public Result queryAllReturnTuition() {
        return returnTuitionService.queryAllReturnTuition();

    }

    /**
     * @param returnTuitionId
     * @return 以JSON格式返回该退班费信息
     * @describe 根据编号查询退班费信息
     */
    @RequestMapping(value = "/returns/{returnTuitionId}", method = RequestMethod.GET)
    public Result queryReturnTuitionById(@PathVariable("returnTuitionId") String returnTuitionId) {
        return returnTuitionService.queryReturnTuitionById(returnTuitionId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加退班费信息
     */
    @RequestMapping(value = "/returns/excels", method = RequestMethod.POST)
    public Result addReturnTuitionByExcel(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        List<ReturnTuition> list = JSON.parseArray(parseObject.getString("json"), ReturnTuition.class);

        return returnTuitionService.addReturnTuitionByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据退班费编号批量删除退班费信息
     */
    @RequestMapping(value = "/returns/dels", method = RequestMethod.POST)
    public Result deleteReturnTuitionByIds(@RequestBody Map<String, String> Ids) {

        return returnTuitionService.deleteReturnTuitionByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 退班费信息模糊查询
     */
    @RequestMapping(value = "/returns/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return returnTuitionService.serachByInfo(info);
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生退班费用的信息
     */
    @RequestMapping(value = "/returns/studentAndReturns", method = RequestMethod.GET)
    public Result queryAllStudentAndReturnInfo() {
        return returnTuitionService.queryAllStudentAndReturnInfo();
    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有学生退班且状态已批准的信息
     */
    @RequestMapping(value = "/returns/studentAndQuitClass", method = RequestMethod.GET)
    public Result queryAllQuitClassAndStudentInfo() {
        return returnTuitionService.queryAllQuitClassAndStudentInfo();
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询退班费用信息
     */
    @RequestMapping(value = "/returns/returnTuitionByUserId", method = RequestMethod.POST)
    public Result selectQuitTuitionByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return returnTuitionService.selectQuitTuitionByUserId(userId);
    }
}
